﻿Imports System.Web.UI
Imports SISSAT.BusinessLayer.DomainModels
Imports SISSAT.Administration.Views
Imports SISSAT.WebDevelopment.Views

Namespace [Shared].UserControls
    Public Class WebUserDetailControl
        Inherits UserControl
        Implements IWebUserDetailControlView

#Region "Fields"

        Private _controlMode As PageModes

#End Region

#Region "Properties"

        Public Property ErrorMessage As String
        Public Property UseControlModeFromUserInterface As Boolean
        Public Property PasswordLabel As String

        Public Property ControlMode As PageModes
            Get
                Return _controlMode
            End Get
            Set(value As PageModes)
                If UseControlModeFromUserInterface Then
                    Exit Property
                End If

                _controlMode = value
                IsReadOnly = Not (_controlMode = PageModes.EditMode)
                CssClass = Convert.ToString(IIf(IsReadOnly, "textbox readonly", "textbox req"))

                InitializeComponents()
            End Set
        End Property

        Public Property ActiveViewIndex As Integer
            Get
                Return ButtonViews.ActiveViewIndex
            End Get
            Set(value As Integer)
                ButtonViews.ActiveViewIndex = value
            End Set
        End Property

        Private Property Presenter As WebUserDetailControlPresenter
        Private Property CssClass As String
        Private Property IsReadOnly As Boolean

#End Region

#Region "IWebUserDetailControlView Members"

        Public ReadOnly Property CurrentUserId As Integer Implements IWebUserDetailControlView.CurrentUserId
            Get
                Return SISSATPageBase.User.Employee.EmployeeId
            End Get
        End Property

        Public ReadOnly Property EmployeeCode As String Implements IWebUserDetailControlView.EmployeeCode
            Get
                Dim code As String = HttpContext.Current.Request.QueryString("code")

                If String.IsNullOrEmpty(code) Then
                    ControlMode = PageModes.EditPartiallyMode
                    code = SISSATPageBase.User.Employee.EmployeeCode
                ElseIf String.Compare(code, "new", True) = 0 Then
                    ControlMode = SISSATPageBase.AppropriatePageModeForUser()
                Else
                    ControlMode = SISSATPageBase.AppropriatePageModeForUser()
                End If

                Return code
            End Get
        End Property

        Public ReadOnly Property EmployeeId As Integer Implements IWebUserDetailControlView.EmployeeId
            Get
                Dim id = 0
                Dim eid As String = HttpContext.Current.Request.QueryString("eid")

                If (Not String.IsNullOrEmpty(eid)) AndAlso Integer.TryParse(eid, id) Then
                    ' No logic
                End If

                Return id
            End Get
        End Property

        Public Event OnClear() Implements IWebUserDetailControlView.OnClear
        Public Event OnInitialization() Implements IWebUserDetailControlView.OnInitialization
        Public Event OnSubmit() Implements IWebUserDetailControlView.OnSubmit
        Public Event OnUpdate() Implements IWebUserDetailControlView.OnUpdate

        Public Property NewPassword As String Implements IWebUserDetailControlView.NewPassword
            Get
                Return PasswordNewTextbox.Text
            End Get
            Set(ByVal value As String)
                PasswordNewTextbox.Text = value
            End Set
        End Property

        Public Property OldPassword As String Implements IWebUserDetailControlView.OldPassword
            Get
                Return PasswordTextBox.Text
            End Get
            Set(ByVal value As String)
                PasswordTextBox.Text = value
            End Set
        End Property

        Public Property Username As String Implements IWebUserDetailControlView.Username
            Get
                Return UsernameTextBox.Text
            End Get
            Set(value As String)
                UsernameTextBox.Text = value
            End Set
        End Property

#End Region

#Region "Methods"

        Private Sub InitializeComponents()
            PasswordLabel = "Password"
            ActiveViewIndex = Convert.ToInt32(IIf(IsReadOnly, 0, 2))
            UsernameTextBox.ReadOnly = IsReadOnly
            UsernameTextBox.CssClass = CssClass
            PasswordTextBox.ReadOnly = IsReadOnly
            PasswordTextBox.CssClass = CssClass

            If ControlMode = PageModes.EditPartiallyMode Then ' Update Mode
                PasswordLabel = "Old Password"
                ActiveViewIndex = 2
                PasswordTextBox.ReadOnly = Not IsReadOnly
                PasswordTextBox.CssClass = "textbox req"
            ElseIf ControlMode = PageModes.EditMode Then ' Administrators Mode
                ActiveViewIndex = 1
                PasswordTextBox.ReadOnly = IsReadOnly
                PasswordTextBox.CssClass = "textbox req"
            End If
        End Sub

#End Region

#Region "Events"

        Private Sub PageInit(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
            Presenter = New WebUserDetailControlPresenter(Me)

            Try
                RaiseEvent OnInitialization()
                ControlMode = PageModes.EditPartiallyMode
            Catch ex As NullReferenceException
                ControlMode = SISSATPageBase.AppropriatePageModeForUser()
            End Try

            InitializeComponents()
        End Sub

        Private Sub NewButton_Click(sender As Object, e As System.EventArgs) Handles NewButton.Click
            Try
                RaiseEvent OnSubmit()
                Me.ErrorMessage = "Save is successful."
            Catch ex As NullReferenceException
                Me.ErrorMessage = "Error on saving data."
            End Try
        End Sub

        Private Sub UpdateButton_Click(sender As Object, e As System.EventArgs) Handles UpdateButton.Click
            Try
                RaiseEvent OnUpdate()
                Me.ErrorMessage = "Save is successful."
            Catch ex As ArgumentNullException
                Me.ErrorMessage = "Old password is incorrect."
            Catch ex As NoNullAllowedException
                Me.ErrorMessage = "Insufficient data. Process aborted."
            End Try
        End Sub

        Private Sub NewClearButton_Click(sender As Object, e As System.EventArgs) Handles NewClearButton.Click
            RaiseEvent OnClear()
            Me.ErrorMessage = "Reset of data is successful."
        End Sub

        Private Sub UpdateClearButton_Click(sender As Object, e As System.EventArgs) Handles UpdateClearButton.Click
            RaiseEvent OnClear()
            Me.ErrorMessage = "Reset of data is successful."
        End Sub

#End Region

    End Class
End Namespace
